package com.easyboot.schedule;

import com.easyboot.utils.log.LogDto;
import com.easyboot.utils.log.LoggerHelper;
import com.easyboot.utils.log.LoggerQueueInstance;
import com.easyboot.web.system.domain.SysLog;
import com.easyboot.web.system.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yushuo
 * @className
 * @descripton TODO
 * @date 2021/6/25 11:30
 **/

@Component
public class EasyLogSchedule {

    @Autowired
    private SysLogService logService;
    @Scheduled(fixedRate =10000)
    public void writeLog(){

        try{

            List<SysLog> logList=new ArrayList<SysLog>(300);
            for (int i=0;i<300;i++){
                LogDto log= LoggerQueueInstance.getLog();
                if(log==null){
                    break;
                }

                SysLog logModel=new SysLog();
                logModel.setLogTitle(log.getLogTitle());
                logModel.setLogMessage(log.getLogMessage());
                logModel.setLogType(log.getLogType());
                logModel.setLogTime(log.getLogDate());
                logList.add((logModel));

            }
            //Date startTime=DateUtil.date();
            if(logList.size()>0){
                logService.batchSaveLog(logList);
            }
           // Date endTime=DateUtil.date();
           // long runMs = endTime.getTime() - startTime.getTime();
           // LoggerHelper.console("开始写入log结束,共耗时："+runMs+" 毫秒");
        }catch(Exception e){
            LoggerHelper.error(e);
            LoggerHelper.console(e.getMessage());
        }


    }
}
